package com.baihan.angus.app.game.busshow.adps;

import android.content.Context;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.baihan.angus.app.game.basic.utils.ComUtil;
import com.baihan.angus.app.game.busshow.R;
import com.baihan.angus.app.game.busshow.pop_dialog.ChatAndCluePopup;
import com.baihan.angus.app.game.common.bean.response.ScriptRole;
import com.baihan.angus.app.game.common.configs.UserConfig;
import com.baihan.angus.app.game.common.util.ImageLoader;
import com.qmuiteam.qmui.widget.QMUIRadiusImageView;

import java.util.List;

public class GameSeatAdp extends RecyclerView.Adapter {

    private Context context;
    private OnItemClickListener mOnItemClickListener;
    private OnPopupItemClickListener mOnPopupItemClickListener;
    private int selectType = 0;
    private ChatAndCluePopup mChatAndCluePopup;
    private List<ScriptRole> list;
    private String userId;

    public static final int SEAT_LEFT = 1;//左侧;
    public static final int SEAT_RIGHT = 2;//右侧;

    public GameSeatAdp(Context context, List<ScriptRole> list, String userId,
                       OnItemClickListener mOnItemClickListener) {
        this.context = context;
        this.list = list;
        this.userId = userId;
        this.mOnItemClickListener = mOnItemClickListener;
//        this.mOnPopupItemClickListener = onPopupItemClickListener;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        Context context = parent.getContext();
        LayoutInflater inflater = LayoutInflater.from(context);
        View view;
        switch (viewType) {
            case SEAT_LEFT:
                if (!UserConfig.getUserConfig().getUser().getUser_id().equals(userId)) {
                    view = inflater.inflate(R.layout.item_game_player_seat_left, parent, false);
                    return new PlayerViewHolder(view);
                } else {
                    view = inflater.inflate(R.layout.item_game_dm_seat_left, parent, false);
                    return new DmViewHolder(view);
                }
            case SEAT_RIGHT:
                if (!UserConfig.getUserConfig().getUser().getUser_id().equals(userId)) {
                    view = inflater.inflate(R.layout.item_game_player_seat_right, parent, false);
                    return new PlayerViewHolder(view);
                } else {
                    view = inflater.inflate(R.layout.item_game_dm_seat_right, parent, false);
                    return new DmViewHolder(view);
                }
        }
        return null;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof PlayerViewHolder) {
            ScriptRole item = list.get(position);
            PlayerViewHolder vh = (PlayerViewHolder) holder;
            vh.bind(context, item, mOnItemClickListener);

//            vh.ivAvatar.setOnClickListener(view -> {
//                if (!GameCache.chooseRoleId.equals(item.getScript_role_id())) {
//                    showPopup(vh, position);
//                }
//            });
        } else if (holder instanceof DmViewHolder) {
            ScriptRole item = list.get(position);
            DmViewHolder vh = (DmViewHolder) holder;
            vh.bind(context, item, mOnItemClickListener);
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (position % 2 == 0) {
            return SEAT_LEFT;
        } else if (position % 2 == 1) {
            return SEAT_RIGHT;
        }
        return 0;
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    private void showPopup(@NonNull RecyclerView.ViewHolder holder, int position) {
//        if (mChatAndCluePopup == null) {
        mChatAndCluePopup = new ChatAndCluePopup(context, selectType, new ChatAndCluePopup.OnSelectListener() {
            @Override
            public void onSelect(int type) {
                selectType = type;
                if (type == 1) {
                    mOnPopupItemClickListener.onSearch(position);
                } else if (type == 2) {
                    mOnPopupItemClickListener.onChat(position);
                }
                mChatAndCluePopup.dismiss();
            }
        });
//        }
        int viewType = getItemViewType(position);
        int[] location = new int[2];
        if (holder instanceof PlayerViewHolder) {
            PlayerViewHolder vh = (PlayerViewHolder) holder;
            vh.ivAvatar.getLocationOnScreen(location);

            if (!mChatAndCluePopup.isShowing()) {
                switch (viewType) {
                    case SEAT_LEFT:
                        mChatAndCluePopup.showAtLocation(vh.ivAvatar,
                                Gravity.NO_GRAVITY,
                                location[0] + vh.ivAvatar.getMeasuredWidth() + ComUtil.Dp2Px(context, 10),
                                location[1]);
                        break;
                    case SEAT_RIGHT:
                        mChatAndCluePopup.showAtLocation(vh.ivAvatar,
                                Gravity.NO_GRAVITY,
                                location[0] - vh.ivAvatar.getMeasuredWidth() - ComUtil.Dp2Px(context, 10),
                                location[1]);
                        break;
                }
            }
        } else if (holder instanceof DmViewHolder) {
            DmViewHolder vh = (DmViewHolder) holder;
            vh.ivAvatar.getLocationOnScreen(location);

            if (!mChatAndCluePopup.isShowing()) {
                switch (viewType) {
                    case SEAT_LEFT:
                        mChatAndCluePopup.showAtLocation(vh.ivAvatar,
                                Gravity.NO_GRAVITY,
                                location[0] + vh.ivAvatar.getMeasuredWidth() + ComUtil.Dp2Px(context, 10),
                                location[1]);
                        break;
                    case SEAT_RIGHT:
                        mChatAndCluePopup.showAtLocation(vh.ivAvatar,
                                Gravity.NO_GRAVITY,
                                location[0] - vh.ivAvatar.getMeasuredWidth() - ComUtil.Dp2Px(context, 10),
                                location[1]);
                        break;
                }
            }
        }
    }

    public static class PlayerViewHolder extends RecyclerView.ViewHolder {

        ImageView ivMicro, ivBorder, ivHand, ivPlayerTalk, ivChat;
        QMUIRadiusImageView ivAvatar;
        TextView tvName;
        LinearLayout llSeat, llMicro;

        public PlayerViewHolder(@NonNull View itemView) {
            super(itemView);
            ivMicro = itemView.findViewById(R.id.ivMicro);
            ivAvatar = itemView.findViewById(R.id.ivAvatar);
            ivHand = itemView.findViewById(R.id.ivHand);
            tvName = itemView.findViewById(R.id.tvName);
            ivBorder = itemView.findViewById(R.id.ivBorder);
            ivPlayerTalk = itemView.findViewById(R.id.ivPlayerTalk);
            ivChat = itemView.findViewById(R.id.ivChat);
            llMicro = itemView.findViewById(R.id.llMicro);
            llSeat = itemView.findViewById(R.id.llSeat);
        }

        public void bind(final Context context,
                         final ScriptRole model,
                         final OnItemClickListener listener) {
            ivMicro.setOnClickListener(view -> {
                listener.onItemClick(getLayoutPosition());
            });
//            ivAudienceAvatar.setOnClickListener(view -> {
//                listener.onAvatar(getLayoutPosition());
//            });
            // 占位图片
            ImageLoader.loadImage(context, ivAvatar, model.avatar, R.mipmap.ic_test_bg);
            if (!TextUtils.isEmpty(model.name)) {
                tvName.setText(model.name);
            } else {
                tvName.setText("加载中");
            }
            if (!model.isUsed) {//玩家未进入座位
                ivBorder.setVisibility(View.GONE);
                llMicro.setVisibility(View.GONE);
                ivMicro.setVisibility(View.GONE);
                tvName.setTextColor(context.getColor(R.color.bg4));
//                ivAudienceAvatar.setImageResource(R.mipmap.ic_test_bg);
//                tvAudienceName.setText("");
//                mIvMute.setVisibility(View.GONE);
            } else {
//                ImageLoader.loadImage(context, ivAudienceAvatar, model.avatar, R.mipmap.ic_test_bg);
//                if (!TextUtils.isEmpty(model.name)) {
//                    tvAudienceName.setText(model.name);
//                } else {
//                    tvAudienceName.setText("加载中");
//                }
                ivBorder.setVisibility(View.VISIBLE);
                llMicro.setVisibility(View.VISIBLE);
                ivMicro.setVisibility(View.VISIBLE);
                ivBorder.setVisibility(View.VISIBLE);
                tvName.setTextColor(context.getColor(R.color.appColor));
                boolean mute = model.isUserMute || model.isSeatMute;
//                ivVoice.setVisibility(mute ? View.VISIBLE : View.GONE);
                ivMicro.setImageResource(mute ? R.mipmap.ic_user_voice_off : R.mipmap.ic_user_voice_on);
                if (mute) {
                    ivPlayerTalk.setVisibility(View.INVISIBLE);
                } else {
                    ivPlayerTalk.setVisibility(model.isTalk ? View.VISIBLE : View.INVISIBLE);
                }

                ivChat.setOnClickListener(v -> {
                    listener.onChat(getLayoutPosition());
                });
            }

            //玩家准备状态
            if (!model.isComplete) {
                ivHand.setVisibility(View.INVISIBLE);
            } else {
                ivHand.setVisibility(View.VISIBLE);
            }
        }
    }

    public static class DmViewHolder extends RecyclerView.ViewHolder {

        ImageView ivMicro, ivHand, ivBorder, ivPlayerTalk, ivChat;
        QMUIRadiusImageView ivAvatar;
        TextView tvName;
        LinearLayout llSeat, llMicro;

        public DmViewHolder(@NonNull View itemView) {
            super(itemView);
            ivMicro = itemView.findViewById(R.id.ivMicro);
            ivHand = itemView.findViewById(R.id.ivHand);
            ivAvatar = itemView.findViewById(R.id.ivAvatar);
            ivBorder = itemView.findViewById(R.id.ivBorder);
            ivPlayerTalk = itemView.findViewById(R.id.ivPlayerTalk);
            ivChat = itemView.findViewById(R.id.ivChat);
            tvName = itemView.findViewById(R.id.tvName);
            llMicro = itemView.findViewById(R.id.llMicro);
            llSeat = itemView.findViewById(R.id.llSeat);
        }

        public void bind(final Context context,
                         final ScriptRole model,
                         final OnItemClickListener listener) {
            llMicro.setOnClickListener(view -> {
                listener.onVoiceMute(getLayoutPosition());
            });
//            ivAudienceAvatar.setOnClickListener(view -> {
//                listener.onAvatar(getLayoutPosition());
//            });
            // 占位图片
            ImageLoader.loadImage(context, ivAvatar, model.avatar, R.mipmap.ic_test_bg);
            if (!TextUtils.isEmpty(model.name)) {
                tvName.setText(model.name);
            } else {
                tvName.setText("加载中");
            }
            if (!model.isUsed) {//玩家未进入座位
                ivBorder.setVisibility(View.GONE);
                ivMicro.setVisibility(View.GONE);
                llMicro.setVisibility(View.GONE);
                tvName.setTextColor(context.getColor(R.color.bg4));
//                ivAudienceAvatar.setImageResource(R.mipmap.ic_test_bg);
//                tvAudienceName.setText("");
//                mIvMute.setVisibility(View.GONE);
            } else {
                ivBorder.setVisibility(View.VISIBLE);
                ivMicro.setVisibility(View.VISIBLE);
                llMicro.setVisibility(View.VISIBLE);
                tvName.setTextColor(context.getColor(R.color.appColor));
                boolean mute = model.isUserMute || model.isSeatMute;
                ivMicro.setImageResource(mute ? R.mipmap.ic_user_voice_off : R.mipmap.ic_user_voice_on);
//                mIvMute.setVisibility(mute ? View.VISIBLE : View.GONE);
                if (mute) {
                    ivPlayerTalk.setVisibility(View.INVISIBLE);
                } else {
                    ivPlayerTalk.setVisibility(model.isTalk ? View.VISIBLE : View.INVISIBLE);
                }

                ivChat.setOnClickListener(v -> {
                    listener.onChat(getLayoutPosition());
                });
            }

            //玩家准备状态
            if (!model.isComplete) {
                ivHand.setVisibility(View.INVISIBLE);
            } else {
                ivHand.setVisibility(View.VISIBLE);
            }
        }
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.mOnItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener {
        void onItemClick(int position);

        void onVoiceMute(int position);

        void onChat(int position);
    }

    public void setOnPopupItemClickListener(OnPopupItemClickListener onPopupItemClickListener) {
        this.mOnPopupItemClickListener = onPopupItemClickListener;
    }

    public interface OnPopupItemClickListener {
        void onSearch(int position);

        void onChat(int position);
    }
}
